package cn.anc.aonicardv.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.MyApplication;

public class SpUtils {

    private static SharedPreferences mPreferences;

    public static SharedPreferences getPreferences() {
        if (mPreferences == null) {
            mPreferences = MyApplication.getInstance().getSharedPreferences(Constant.SpKeyParam.CONFIG,
                    Context.MODE_PRIVATE);
        }
        return mPreferences;
    }

    /**
     * 保存boolean值
     *
     * @param key
     * @param value
     */
    public static void putBoolean(String key, boolean value) {
        SharedPreferences sp = getPreferences();
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * 获取sp里面保存的boolean值
     *
     * @param key
     * @param defValue
     * @return
     */
    public static boolean getBoolean(String key, boolean defValue) {
        SharedPreferences sp = getPreferences();
        return sp.getBoolean(key, defValue);
    }

    /**
     * 保存字符串值
     *
     * @param key
     * @param value
     */
    public static void putString(String key, String value) {
        SharedPreferences sp = getPreferences();
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    /**
     * 获取sp里面保存的字符串
     *
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(String key, String defValue) {
        SharedPreferences sp = getPreferences();

        return sp.getString(key, defValue);
    }

    public static void putInt(String key, int which) {
        SharedPreferences sp = getPreferences();
        Editor editor = sp.edit();
        editor.putInt(key, which);
        editor.commit();
    }

    public static void putLong(String key, long which) {
        SharedPreferences sp = getPreferences();
        Editor editor = sp.edit();
        editor.putLong(key, which);
        editor.commit();
    }

    public static int getInt(String key, int defValue) {
        SharedPreferences sp = getPreferences();
        return sp.getInt(key, defValue);
    }

    public static long getLong(String key, int defValue) {
        SharedPreferences sp = getPreferences();
        return sp.getLong(key, defValue);
    }

    public static void remove(String key) {
        getPreferences().edit().remove(key).commit();
    }

    public static void clear() {
        getPreferences().edit().clear().commit();
    }

}
